﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LIM.controller;
using LIM.model;

namespace LIM.forms.uc_CRM_PhieuNhanMau_ChiTiet
{
    public partial class uc_CRM_PhieuNhanMau_ChiTiet_ThongTinBenNhan : UserControl
    {
        radForm_CRM_PhieuNhanMau_ChiTiet parentForm = null;

        public uc_CRM_PhieuNhanMau_ChiTiet_ThongTinBenNhan()
        {
            InitializeComponent();            
        }

        public void DelegateEvent_Load()
        {
            parentForm = (radForm_CRM_PhieuNhanMau_ChiTiet)this.ParentForm;
            if (parentForm != null)
            {
                parentForm.OnLuuLai_Click += new radForm_CRM_PhieuNhanMau_ChiTiet.LuuLai_Click(parentForm_OnLuuLai_Click);
                parentForm.OnKhachHang_Changed += new radForm_CRM_PhieuNhanMau_ChiTiet.KhachHang_Changed(parentForm_OnKhachHang_Changed);                
            }
        }

        private void uc_CRM_HopDong_ChiTiet_ThongTinBenNhan_Load(object sender, EventArgs e)
        {
            if (parentForm.tbl_CRM_PhieuNhanMau != null && parentForm.tbl_CRM_PhieuNhanMau.RowID > 0)
            {
                this.radTextBox_TenKhachHang.Text = parentForm.tbl_CRM_PhieuNhanMau.TenKhachHang;
                this.radTextBox_Fax.Text = parentForm.tbl_CRM_PhieuNhanMau.SoFax;
                this.radTextBox_TaiKhoanNganHang.Text = parentForm.tbl_CRM_PhieuNhanMau.TaiKhoanNganHang;
                this.radTextBox_MaSoThue.Text = parentForm.tbl_CRM_PhieuNhanMau.MaSoThue;
                this.radTextBox_DiaChi.Text = parentForm.tbl_CRM_PhieuNhanMau.DiaChi;

                LienHe_Load(parentForm.tbl_CRM_PhieuNhanMau.RowIDLienLac.GetValueOrDefault(0));

                this.radRadioButton_DienThoaiCongTy.IsChecked = !(this.radRadioButton_DienThoaiCaNhan.IsChecked = parentForm.tbl_CRM_PhieuNhanMau.LaSoDienThoaiCaNhan.GetValueOrDefault(false));
                this.radRadioButton_EmailCongTy.IsChecked = !(this.radRadioButton_EmailCaNhan.IsChecked = parentForm.tbl_CRM_PhieuNhanMau.LaEmailCaNhan.GetValueOrDefault(false));

                if (parentForm.tbl_CRM_PhieuNhanMau.LaSoDienThoaiCaNhan.GetValueOrDefault(false))
                {
                    this.radRadioButton_DienThoaiCaNhan.IsChecked = true;
                    this.radTextBox_DienThoaiCaNhan.Text = parentForm.tbl_CRM_PhieuNhanMau.SoDienThoai;
                }
                else
                {
                    this.radRadioButton_DienThoaiCongTy.IsChecked = true;
                    this.radTextBox_DienThoaiCongTy.Text = parentForm.tbl_CRM_PhieuNhanMau.SoDienThoai;
                }

                if (parentForm.tbl_CRM_PhieuNhanMau.LaEmailCaNhan.GetValueOrDefault(false))
                {
                    this.radRadioButton_EmailCaNhan.IsChecked = true;
                    this.radTextBox_EmailCaNhan.Text = parentForm.tbl_CRM_PhieuNhanMau.Email;
                }
                else
                {
                    this.radRadioButton_EmailCongTy.IsChecked = true;
                    this.radTextBox_EmailCongTy.Text = parentForm.tbl_CRM_PhieuNhanMau.Email;
                }                
            }
        }

        void parentForm_OnKhachHang_Changed()
        {                        
            DataLoad();
        }

        private void DataLoad()
        {
            if(parentForm.KhachHangInfo!=null)
            {
                this.radTextBox_TenKhachHang.Text = parentForm.KhachHangInfo.TenGoi;                
                this.radTextBox_TaiKhoanNganHang.Text = parentForm.KhachHangInfo.TaiKhoanNganHang;
                this.radTextBox_MaSoThue.Text = parentForm.KhachHangInfo.MaSoThue;
                this.radTextBox_DiaChi.Text = parentForm.KhachHangInfo.DiaChi;
                this.radTextBox_Fax.Text = parentForm.KhachHangInfo.SoFax;

                this.radDropDownList_NguoiLienHe.DataSource = null;
                
                this.radTextBox_DienThoaiCongTy.Text = "";
                this.radTextBox_EmailCongTy.Text = "";
                this.radTextBox_DienThoaiCaNhan.Text = "";
                this.radTextBox_EmailCaNhan.Text = "";
                LienHe_Load();
            }
        }

        void parentForm_OnLuuLai_Click()
        {
            if (parentForm.tbl_CRM_PhieuNhanMau != null)
            {
                parentForm.tbl_CRM_PhieuNhanMau.TenKhachHang = this.radTextBox_TenKhachHang.Text;
                parentForm.tbl_CRM_PhieuNhanMau.DiaChi = this.radTextBox_DiaChi.Text;
                parentForm.tbl_CRM_PhieuNhanMau.SoFax = this.radTextBox_Fax.Text;
                if (this.radRadioButton_DienThoaiCaNhan.IsChecked)
                    parentForm.tbl_CRM_PhieuNhanMau.SoDienThoai = this.radTextBox_DienThoaiCaNhan.Text;
                else parentForm.tbl_CRM_PhieuNhanMau.SoDienThoai = this.radTextBox_DienThoaiCongTy.Text;
                if (this.radRadioButton_EmailCaNhan.IsChecked)
                    parentForm.tbl_CRM_PhieuNhanMau.Email = this.radTextBox_EmailCaNhan.Text.Trim();
                else parentForm.tbl_CRM_PhieuNhanMau.Email = this.radTextBox_EmailCongTy.Text.Trim();
                parentForm.tbl_CRM_PhieuNhanMau.RowIDLienLac = Convert.ToInt32(this.radDropDownList_NguoiLienHe.SelectedValue);
                parentForm.tbl_CRM_PhieuNhanMau.HoTenNguoiDaiDien = this.radDropDownList_NguoiLienHe.SelectedText;
                parentForm.tbl_CRM_PhieuNhanMau.MaSoThue = this.radTextBox_MaSoThue.Text;
                parentForm.tbl_CRM_PhieuNhanMau.TaiKhoanNganHang = this.radTextBox_TaiKhoanNganHang.Text;
            }
        }

        private void LienHe_Load(int selectedRowID = 0)
        {
            if (parentForm.KhachHangInfo != null && parentForm.KhachHangInfo.RowID > 0)
            {
                DanhMucLienHeController DanhMucLienHeController = new controller.DanhMucLienHeController();
                tbl_CRM_KhachHang tbl_CRM_KhachHang = DanhMucLienHeController.LoadKhachHangByID(parentForm.KhachHangInfo.RowID);

                this.radDropDownList_NguoiLienHe.DisplayMember = "tbl_CRM_LienHe.HoTen";
                this.radDropDownList_NguoiLienHe.ValueMember = "RowIDLienHe";
                this.radDropDownList_NguoiLienHe.DataSource = tbl_CRM_KhachHang.tbl_CRM_LienHeKhachHang;

                if (selectedRowID > 0)
                {
                    radDropDownList_NguoiLienHe.SelectedValue = selectedRowID;
                }                
            }
        }

        private void radDropDownList_NguoiLienHe_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (radDropDownList_NguoiLienHe.SelectedItem != null)
            {
                tbl_CRM_LienHeKhachHang tbl_CRM_LienHeKhachHang = (tbl_CRM_LienHeKhachHang)radDropDownList_NguoiLienHe.SelectedItem.DataBoundItem;
                if (tbl_CRM_LienHeKhachHang != null)
                {
                    this.radTextBox_Fax.Text = tbl_CRM_LienHeKhachHang.SoFax;
                    this.radTextBox_DienThoaiCongTy.Text = tbl_CRM_LienHeKhachHang.SoDienThoai;
                    this.radTextBox_EmailCongTy.Text = tbl_CRM_LienHeKhachHang.Email;

                    this.radTextBox_DienThoaiCaNhan.Text = tbl_CRM_LienHeKhachHang.tbl_CRM_LienHe.SoDiDong;
                    this.radTextBox_EmailCaNhan.Text = tbl_CRM_LienHeKhachHang.tbl_CRM_LienHe.EmailCaNhan;
                }
            }
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {
            uc_CRM_HopDong_ChiTiet_ThongTinBenNhan_Load(sender, e);
        }
    }
}
